package com.jingbit.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CustomListDialog extends CustomBaseDialog {

    private Button no;//取消按钮
    private TextView titleView;
    private List<String> list;
    private ArrayAdapter arrayAdapter;
    private IItemClickListener itemClickListener;//确定按钮被点击了的监听器

    public CustomListDialog(Context context) {
        super(context, R.style.CustomDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("jingbit", "onCreate");
        setContentView(R.layout.activity_list);
        //初始化界面控件
        no = findViewById(R.id.btn_cancel);
        titleView = findViewById(R.id.tv_title);
        ListView lv_content = findViewById(R.id.lv_content);
        arrayAdapter = new ArrayAdapter(getContext(), R.layout.activity_list_item, list);
        lv_content.setAdapter(arrayAdapter);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(position);
                }
            }
        });

        //设置取消按钮被点击后，向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show(String title, List<String> list, IItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        this.list = list == null ? new ArrayList<>() : list;
        showDialog(Gravity.CENTER, R.style.AnimCenter, 0.9f, 0);
        if (!TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }
        arrayAdapter.notifyDataSetChanged();
    }

    public interface IItemClickListener {
        public void onItemClick(int position);
    }
}